package com.zhongxi.guoyuan.view;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.fragment.MainCoinFragment;
import com.zhongxi.guoyuan.fragment.MainPersonFragment;
import com.zhongxi.guoyuan.fragment.MainRewardCoinFragment;
import com.zhongxi.guoyuan.fragment.MainStoreFragment;
import com.zhongxi.guoyuan.view.user.LoginActivity;

public class MainActivity extends BaseActivity {

    private RelativeLayout relaContainer;
    private Button[] tabs;
    private Fragment[] fragments;
    private MainStoreFragment firstFragment;
    private MainCoinFragment thirdFragment;
    private MainRewardCoinFragment secondFragment;
    private MainPersonFragment forthFragment;
    private int currentIndex = 0, index=-1;
    private RelativeLayout relaHome;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_main);
        relaContainer = (RelativeLayout) findViewById(R.id.relaContainer);
        tabs = new Button[4];
        tabs[0] = (Button) findViewById(R.id.tab0);
        tabs[1] = (Button) findViewById(R.id.tab1);
        tabs[2] = (Button) findViewById(R.id.tab2);
        tabs[3] = (Button) findViewById(R.id.tab3);
        relaHome = (RelativeLayout) findViewById(R.id.relaHome);

        firstFragment = new MainStoreFragment();
//        secondFragment = new SellerFragment();
        secondFragment = new MainRewardCoinFragment();
        thirdFragment = new MainCoinFragment();
        forthFragment = new MainPersonFragment();
        fragments = new Fragment[]{firstFragment, secondFragment, thirdFragment, forthFragment};

        index = getIntent().getIntExtra("index", -1);
        if(index == 0){
            getSupportFragmentManager().beginTransaction().add(R.id.relaContainer, fragments[0]).commit();
            tabs[0].setSelected(true);
        }else{
            refresh();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        relaHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void refresh() {
        if(index != currentIndex){
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if(fragments[currentIndex].isAdded()){
                trx.hide(fragments[currentIndex]);
            }
            if(!fragments[index].isAdded()){
                trx.add(R.id.relaContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.tab0:
                index = 0;
                break;
            case R.id.tab1:
                index = 1;
                break;
            case R.id.tab2:
                index = 2;
                break;
            case R.id.tab3:
                if(null==MyApplication.getInstance().getUserId()||"".equals(MyApplication.getInstance().getUserId())){
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.putExtra("position", 1);
                    startActivityForResult(intent, 100);
                }else{
                    index = 3;
                }
                break;
        }
        refresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            if(null!=MyApplication.getInstance().getUserId()&&!"".equals(MyApplication.getInstance().getUserId())){
                index = 3;
                refresh();
            }
        }
    }
}
